<?php
apf_require_class("Bll_BLL");
/**
 * 与lumesse建立链接
 * @author dinglin
 */
class Bll_Flow_Lumesse extends BLL{
    private static $lumesse;
    private function __clone(){}
    private static $instance = NULL;
    /**
     * @return Bll_Flow_Lumesse
     */
    public static function get_instance(){
        if(self::$instance == NULL){
            self::$lumesse = APF::get_instance()->get_config('lumesse','common');
            self::$instance = new Bll_Flow_Lumesse();
        }
        return self::$instance;;
    }
    /**
     * @return Lumesse_CareerPortal
     */
    public function Lumesse_CareerPortal(){
        return Lumesse_CareerPortal::get_instance(self::$lumesse['CareerPortal']);
    }
    /**
     * @return Lumesse_HRIS
     */
    public function Lumesse_HRIS(){
        return Lumesse_HRIS::get_instance(self::$lumesse['HRIS']);
    }
    /**
     * @return Lumesse_User
     */
    public function Lumesse_User(){
        return Lumesse_User::get_instance(self::$lumesse['User']);
    }
    /**
     * @return Lumesse_Posting
     */
    public function Lumesse_Posting(){
        return Lumesse_Posting::get_instance(self::$lumesse['Posting']);
    }
    /**
     * 查询lumesse用户信息
     * @param int $user_id
     * @return array
     */
    public function get_user_by_id($user_id){
        if(!$user_id || !is_numeric($user_id)){
            return array();
        }
        return $this->get_default_dao()->find(array('user_id'=>$user_id));
    }
    private $allow_dep=array();
    /**
     * 递归分类
     * @param array $table
     * @param int $p_id
     * @return multitype:boolean
     */
    public function tree($table,$p_id,$is_allow=false) {
        $tree = array();
        foreach($table as $id => $row){
            if($row['lums_pdepart_id']==$p_id){
                if($is_allow){
                    $this->allow_dep[]=$p_id;
                }
                $tmp = $this->tree($table,$row['lums_depart_id']);
                if($tmp){
                    if($is_allow){
                        $this->allow_dep[] = $row['lums_depart_id'];
                        foreach($tmp as $v){
                            $this->allow_dep[]=$v['lums_depart_id'];
                        }
                    }
                    $row['children']=$tmp;
                }else{
                    if($is_allow){
                        $this->allow_dep[]=$row['lums_depart_id'];
                    }
                    $row['leaf'] = true;
                }
                unset($table[$id]);
                $tree[]=$row;
            }
        }
        return $tree;
    }

    public function getcategory($dep_list,$is_allow) {
        $tree = new Tree($type);
        $space='|----';
        $miniupid = '';
        $delbase=0;
        //$delid = array();
        //if($delbase) {
        //    $delid[] = $delbase;
        //}

        foreach($dep_list as $value){
            if($miniupid == '') $miniupid = $value['lums_pdepart_id'];
            $tree->setNode($value['lums_depart_id'], $value['lums_pdepart_id'], $value);
        }
        $listarr = array();
        $categoryarr = $tree->getChilds($miniupid);

        foreach ($categoryarr as $key => $catid) {
            $cat = $tree->getValue($catid);
            $cat['pre'] = $tree->getLayer($catid, $space);
            if(!empty($is_allow) && (in_array($cat['lums_pdepart_id'], $is_allow) || $cat['lums_depart_id'] == $delbase)) {
                $delid[] = $cat['lums_depart_id'];
            } else {
                $listarr[$cat['lums_depart_id']] = $cat;
            }
        }
        return $listarr;

    }
    /**
     * 获取一个员工有lumesse所有招聘的部门
     * @param unknown_type $user_id
     * @return multitype:
     */
    public function get_user_luemsse_department($user_id){
        $data = array();
        $dep_list = $this->get_lus_dep_dao()->find(array(),0,'');
        $st = $this->getcategory($dep_list);
        //一个人可能有多条
        $user_data = $this->get_user_by_id($user_id);
        if(empty($user_data)){
            return $data;
        }
        //整理子级部门 TODO
        foreach($user_data as $val){
            $ls = $this->tree($dep_list, $val['lums_depart_id'],1);
            if($val['lums_depart_id']){
                $this->allow_dep[]=$val['lums_depart_id'];
            }
        }
        foreach($st as $val){
            if(in_array($val['lums_depart_id'],$this->allow_dep)){
                 $data[$val['lums_depart_id']] = $val;
            }
        }
        return $data;
    }
    /**
     * @return Dao_Flow_Lumesse
     */
    private function get_default_dao(){
        return $this->get_dao("Dao_Flow_Lumesse");
    }
    /**
     *
     * @return Ambigous <Aifang_Core_DAO, NULL>
     */
    private function get_lus_dep_dao(){
        return $this->get_dao("Dao_Flow_LumesseDepartment");
    }
}
class Tree {
    var $data = array();
    var $child = array(-1 => array());
    var $layer = array(-1 => -1);
    var $parent = array();
    var $countid = 0;
    function Tree($value) {}

    function setNode($id, $parent, $value) {

        $parent = $parent?$parent:0;

        $this->data[$id] = $value;
        $this->child[$parent][]  = $id;
        $this->parent[$id] = $parent;

        if(!isset($this->layer[$parent])) {
            $this->layer[$id] = 0;
        } else {
            $this->layer[$id] = $this->layer[$parent] + 1;
        }
    }

    function getList(&$tree, $root= 0) {
        foreach($this->child[$root] as $key => $id) {
            $tree[] = $id;
            if($this->child[$id]) $this->getList($tree, $id);
        }
    }

    function getValue($id) {
        return $this->data[$id];
    }
    function reSetLayer($id) {
        if($this->parent[$id]) {
            $this->layer[$this->countid] = $this->layer[$this->countid] + 1;
            $this->reSetLayer($this->parent[$id]);
        }
    }
   function getLayer($id, $space = false) {
        $this->layer[$id] = 0;
        $this->countid = $id;
        $this->reSetLayer($id);
        return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
    }
    function getParent($id) {
        return $this->parent[$id];
    }
    function getParents($id) {
        while($this->parent[$id] != -1) {
            $id = $parent[$this->layer[$id]] = $this->parent[$id];
        }
        ksort($parent);
        reset($parent);
        return $parent;
    }
    function getChild($id) {
        return $this->child[$id];
    }
    function getChilds($id = 0) {
        $child = array();
        $this->getList($child, $id);
        return $child;
    }
}
/**
 * CareerPortal
 *
 * @author dinglin
 *
 */
class Lumesse_CareerPortal{
    private function __construct(){}
    private function __clone(){}
    private static $instance = NULL;
    private static $lumesse;
    /**
     *
     * @return Lumesse_CareerPortal
     */
    public static function get_instance($lumesse){
        if(self::$instance == NULL){
            self::$instance = new Lumesse_CareerPortal();
            self::$lumesse = $lumesse;
        }
        return self::$instance;;
    }
    /**
     * SearchAgent
     *
     * @return SoapClient
     */
    public function SearchAgent(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'SearchAgent');
    }
    /**
     * FoAdvert
     *
     * @return SoapClient
     */
    public function FoAdvert(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'TalentPortal');
    }
    /**
     * TalentPortal
     *
     * @return SoapClient
     */
    public function TalentPortal(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'TalentPortal');
    }
}
/**
 * HRIS
 *
 * @author dinglin
 *
 */
class Lumesse_HRIS{
    private function __construct(){}
    private function __clone(){}
    private static $instance = NULL;
    private static $lumesse;
    /**
     *
     * @return Lumesse_CareerPortal
     */
    public static function get_instance($lumesse){
        if(self::$instance == NULL){
            self::$instance = new Lumesse_HRIS();
            self::$lumesse = $lumesse;
        }
        return self::$instance;;
    }
    /**
     * Position
     *
     * @return SoapClient
     */
    public function Position(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Position');
    }
    /**
     * Candidate
     *
     * @return SoapClient
     */
    public function Candidate(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Candidate');
    }
    /**
     * Queue
     *
     * @return SoapClient
     */
    public function Queue(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Queue');
    }
    /**
     * Document
     *
     * @return SoapClient
     */
    public function Document(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Document');
    }
}
/**
 * User
 *
 * @author dinglin
 *
 */
class Lumesse_User{
    private function __construct(){}
    private function __clone(){}
    private static $instance = NULL;
    private static $lumesse;
    /**
     *
     * @return Lumesse_CareerPortal
     */
    public static function get_instance($lumesse){
        if(self::$instance == NULL){
            self::$instance = new Lumesse_User();
            self::$lumesse = $lumesse;
        }
        return self::$instance;;
    }
    /**
     * User
     *
     * @return SoapClient
     */
    public function User(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'User');
    }
    /**
     * Role
     *
     * @return SoapClient
     */
    public function Role(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Role');
    }
    /**
     * Organization
     *
     * @return SoapClient
     */
    public function Organization(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Organization');
    }
}
/**
 * User
 *
 * @author dinglin
 *
 */
class Lumesse_Posting{
    private function __construct(){}
    private function __clone(){}
    private static $instance = NULL;
    private static $lumesse;
    /**
     *
     * @return Lumesse_CareerPortal
     */
    public static function get_instance($lumesse){
        if(self::$instance == NULL){
            self::$instance = new Lumesse_Posting();
            self::$lumesse = $lumesse;
        }
        return self::$instance;;
    }
    /**
     * Posting
     *
     * @return SoapClient
     */
    public function Posting(){
        return Base_soap::get_instance()->create_soap(self::$lumesse, 'Posting');
    }
}
/**
 * soap 链接lumesse
 * @author dinglin
 */
class Base_soap{
    private function __construct(){}
    private function __clone(){}
    private static $instance = NULL;
    /**
     *
     * @return Base_soap
     */
    public static function get_instance(){
        if(self::$instance == NULL){
            self::$instance = new Base_soap();
        }
        return self::$instance;;
    }
    /**
     * @return SoapClient
     */
    public function create_soap($lumesse,$method){
        $_wsSecurityNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
        $soap_Username = new SoapVar($lumesse['USER_NAME'], XSD_STRING, NULL, $_wsSecurityNS, NULL, $_wsSecurityNS);
        $soap_Password = new SoapVar($lumesse['USER_PWD'], XSD_STRING, NULL, $_wsSecurityNS, NULL, $_wsSecurityNS);
        $soap_Auth = new WSSEAuth($soap_Username, $soap_Password);
        $soapVar_Auth = new SoapVar($soap_Auth, SOAP_ENC_OBJECT, NULL, $_wsSecurityNS, 'UsernameToken', $_wsSecurityNS);
        $soap_Auth_Token = new WSSEToken($soapVar_Auth);
        $soapVar_Auth_Token = new SoapVar($soap_Auth_Token, SOAP_ENC_OBJECT, NULL, $_wsSecurityNS, 'UsernameToken', $_wsSecurityNS);
        $soapVar_Security = new SoapVar($soapVar_Auth_Token, SOAP_ENC_OBJECT, NULL, $_wsSecurityNS, 'Security', $_wsSecurityNS);
        $soapVar_Header = new SoapHeader($_wsSecurityNS, 'Security', $soapVar_Security, true, "TlkPrincipal");
        try{
            $ws = @new SoapClient($lumesse['BASE_URL'].$method.'?wsdl');
            $ws->__setSoapHeaders(array($soapVar_Header));
            $ws->__setLocation($lumesse['BASE_URL'].$method. '?api_key=' . $lumesse['API_KEY']);
            return $ws;
        }
        catch (Exception $e){
            //上线关闭
            echo $e->getMessage();
            return null;
        }
    }
}

// additional classes to facilitate WSSE addition to standard SOAP for PHP
class WSSEAuth
{
    private $Username;
    private $Password;

    function __construct($username, $password)
    {
        $this->Username = $username;
        $this->Password = $password;
    }
}

class WSSEToken
{
    private $UsernameToken;

    function __construct ($token)
    {
        $this->UsernameToken = $token;
    }
}